package study.springcloud.breaker.sentinel.controller._1_flow;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.eggs.result.Result;
import study.eggs.result.Results;

import java.util.Map;

@Slf4j
@RestController
public class _2_FlowController {

    @SentinelResource(blockHandler = "myBlockHandler")
    @RequestMapping("/flow2")
    public Result echo() {
        Map<String, Object> data = Maps.newHashMap();
        data.put("msg", "echo2 echo2 echo2");
        return Results.ok(data);
    }

    public Result myBlockHandler(BlockException ex) {
        ex.printStackTrace();
        ex.getRule();
        Map<String, Object> data = Maps.newHashMap();
        data.put("message", "该接口请求太多，被限流...");
        return Results.ok(data);
    }
}
